package com.coocaa.typhoon.api.v1.userfile.provider;

import cn.hutool.core.io.file.FileNameUtil;
import com.coocaa.cirrus.common.third.filestore.config.TosProperties;
import com.coocaa.typhoon.api.third.volcengine.service.TOSService;
import com.coocaa.typhoon.api.v1.userfile.constant.FileSceneConstant;
import com.coocaa.typhoon.api.v1.userfile.model.UploadTmpResult;
import com.coocaa.typhoon.api.v1.userfile.util.TosFinalUrlUtil;
import com.coocaa.typhoon.api.v1.userfile.util.TosUploadFilePathUtil;
import com.drew.imaging.FileType;
import net.coobird.thumbnailator.Thumbnails;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;


import java.io.*;

/**
 * @author bijiahao
 * @date : 2025/9/19 14:13
 * @description
 */
@Service
public class PdfFileUploadProvider {
    private static final int DPI = 150;               // 清晰度，150 足够预览
    private static final Float COMPRESS_QUALITY = 0.85f;   // 图片质量 85%
    @Autowired
    private TOSService tosService;
    @Autowired
    private TosProperties tosProperties;

    public List<UploadTmpResult> splitUploadImage(InputStream pdfStream, UploadTmpResult uploadTmpResult) throws IOException {
        List<UploadTmpResult> res = new ArrayList<>();
        try (PDDocument doc = PDDocument.load(pdfStream)) {
            PDFRenderer renderer = new PDFRenderer(doc);
            for (int i = 0; i < doc.getNumberOfPages(); i++) {
                BufferedImage image = renderer.renderImageWithDPI(i, DPI, ImageType.RGB);
                // 压缩
                ByteArrayOutputStream imgOut = new ByteArrayOutputStream();
                String extName = FileType.Png.getName().toLowerCase();
                Thumbnails.of(image)
                        .scale(1.0)              // 不缩放
                        .outputQuality(COMPRESS_QUALITY)
                        .outputFormat(extName)
                        .toOutputStream(imgOut);
                String remoteUploadFilePath = TosUploadFilePathUtil.getRemoteUploadFilePathForRlsFile(tosProperties.getTosEnvDir(), uploadTmpResult.getFileName(), i, extName, FileSceneConstant.PREVIEW);
                tosService.uploadFile(imgOut, remoteUploadFilePath);
                UploadTmpResult tmpResult = UploadTmpResult.builder()
                        .fileExtName(extName.toUpperCase())
                        .fileName(FileNameUtil.getName(remoteUploadFilePath))
                        .tosLink(TosFinalUrlUtil.getTosLink(tosProperties.getTosBucketName(), remoteUploadFilePath))
                        .url(TosFinalUrlUtil.getHttpUrl(tosProperties.getTosUrlPrefix(), remoteUploadFilePath))
                        .build();
                res.add(tmpResult);
            }
        }
        return res;
    }
}
